import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutterproject/mvvm/base/base_state.dart';
import 'package:flutterproject/page/red_package/controller/red_package_controller.dart';
import 'package:flutterproject/page/red_package/widget/deng.dart';
import 'package:flutterproject/util/extension/extension_util.dart';
import 'package:flutterproject/util/extension/widget_extension.dart';
import 'package:flutterproject/util/margin_padding_util.dart';
import 'package:flutterproject/widget/image_extended.dart';
import 'package:flutterproject/widget/spinning_wheel/spinning_wheel.dart';
import 'package:flutterproject/widget/text/text_common.dart';
import 'package:get/get.dart';

class RedPackagePage extends BaseState<RedPackageController> {
  @override
  RedPackageController initController() => RedPackageController();

  @override
  Widget initView(BuildContext context) {
    return Container(
      color: Color(0xFFFCA969),
      child: SingleChildScrollView(
        child: Column(
          children: [
            47.heightBox,
            ImageCommon(
              "/image/ic_redfont",
              width: 640.width,
              height: 415.width,
            ),
            20.heightBox,
            SizedBox(
              width: Get.width,
              child: Stack(
                clipBehavior: Clip.none,
                alignment: Alignment.topCenter,
                children: [
                  Positioned(
                    top: -280.width,
                    child: ImageCommon(
                      "/image/ic_redbg1",
                      width: Get.width,
                      fit: BoxFit.contain,
                    ),
                  ),
                  Container(
                    width: 600.width,
                    height: 600.width,
                    decoration: BoxDecoration(
                      image: DecorationImage(
                        image: AssetImage('assets/image/ic_pan.png'),
                        fit: BoxFit.fill, // 完全填充
                      ),
                    ),
                    child: Stack(
                      alignment: Alignment.center,
                      children: [
                        SpinningWheel(
                          Image.asset('assets/image/red_package_1.png'),
                          key: controller.childKey,
                          width: 470.width,
                          height: 470.width,
                          initialSpinAngle: 0,
                          spinResistance: 0.6,
                          canInteractWhileSpinning: false,
                          dividers: 4,
                          onEnd: () {
                            controller.showGiftDialog();
                          },
                        ),
                        DengPage(
                          key: controller.childKey2,
                        ),
                        ImageCommon(
                          "/image/red_package_2",
                          width: 196.width,
                          height: 214.width,
                          onPress: () {
                            controller.pick();
                          },
                        )
                      ],
                    ),
                  ),
                ],
              ),
            ),
            100.heightBox,
            GetBuilder<RedPackageController>(
                id: "tab",
                builder: (logic) {
                  return Container(
                    margin: MarginPaddingUtil.only(left: 47, right: 38),
                    padding:
                        MarginPaddingUtil.symmetric(vertical: 6, horizontal: 8),
                    decoration: BoxDecoration(
                      color: Color(0xFFEE9B5C),
                      borderRadius: BorderRadius.circular(20.radius),
                    ),
                    child: Row(
                      children: [
                        _buildWord("ic_gong", 0),
                        10.widthBox,
                        _buildWord("ic_xi", 1),
                        10.widthBox,
                        _buildWord("ic_fa", 2),
                        10.widthBox,
                        _buildWord("ic_cai", 3),
                      ],
                    ),
                  );
                }),
            35.heightBox,
            Stack(
              clipBehavior: Clip.none,
              children: [
                Container(
                  height: 80.width,
                  margin: MarginPaddingUtil.symmetric(horizontal: 80),
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(54.radius),
                    gradient: LinearGradient(
                      colors: [
                        Color(0xFFFF6464),
                        Color(0xFFEE8B36),
                      ],
                    ),
                  ),
                  child: Row(
                    children: [
                      72.widthBox,
                      TextCommon(
                        "1",
                        size: 50.sp,
                        fontWeight: FontWeight.w500,
                        color: Colors.white,
                      ),
                      Spacer(),
                      TextCommon(
                        "今日剩余抽奖次数",
                        size: 24.sp,
                        lineHeight: 30.sp,
                        fontWeight: FontWeight.w500,
                        color: Colors.white,
                      ),
                      Spacer(),
                      TextCommon(
                        "${controller.times}",
                        size: 50.sp,
                        fontWeight: FontWeight.w500,
                        color: Colors.white,
                      ),
                      72.widthBox,
                    ],
                  ),
                ),
                Positioned(
                  top: -25.width,
                  left: 82.width,
                  child: ImageCommon(
                    "/icon/ic_red3",
                    size: 128.width,
                  ),
                )
              ],
            )
          ],
        ),
      ),
    );
  }

  Widget _buildWord(String value, int index) {
    return Expanded(
      child: Container(
        alignment: Alignment.center,
        padding: MarginPaddingUtil.symmetric(vertical: 16, horizontal: 0),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(14.radius),
          color: index != controller.currentIndex ? Color(0xFFFC7A5D) : null,
          gradient: index == controller.currentIndex
              ? LinearGradient(
                  colors: [Color(0xFFFFBEBE), Color(0xFFFE2828)],
                  begin: Alignment.topCenter,
                  end: Alignment.bottomCenter,
                )
              : null,
        ),
        child: ImageCommon(
          "/icon/$value",
          width: 36.width,
          height: 44.height,
        ),
      ).addClickEvent(() => controller.changeIndex(index)),
    );
  }
}
